#!/usr/bin/python3
import socket
from threading import Thread
import struct
import re
import time
from enum import Enum

client_conn_pool = [] #客户端连接的线程池
main_socket_server = None #负责监听的socket
ADDRESS = ('192.168.1.102',2333)

trj_ser_list = [[],[]]
clientall_state = [0,0,0]

def init():
	global main_socket_server
	global client1_recv_flag
	global client2_recv_flag
	global main_stj1_recv
	global main_stj2_recv
	global msg_len
	client1_recv_flag = False
	client2_recv_flag = False
	main_stj1_recv = True
	main_stj2_recv = True
	main_socket_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
#	main_socket_server(socket.SOL_SOCKET, socket.SO_REUSEADDR,1)
	main_socket_server.bind((ADDRESS))
	main_socket_server.listen(5) #最大等待数 不是连接数
	print('服务端已经启动，等待客户端的连接......')

def accept_client():
	while True:
		client,c_addr = main_socket_server.accept() #阻塞，等待客户端连接
		client_conn_pool.append(client)
		thread = Thread(target=message_handle,args=(client,))
		thread.setDaemon(True)
		thread.start()

def message_handle(client):
	global main_socket_server
	global client1_recv_flag
	global client2_recv_flag
	global main_stj1_recv
	global clt_ID
	clt_ID = [0,0,0] 
	global main_stj2_recv
	global msg_len
	msg_len = 100
#	client.sendall('连接服务器`成功'.encode(encoding='utf-8'))
	while True:
		#print(client1_recv_flag)
		re_bytes = client.recv(20)
		client_ID,data_number,msg_len,pose_x,pose_y = struct.unpack('!3i2f',re_bytes)
		print(client1_recv_flag,client_ID,data_number,msg_len,pose_x,pose_y)
		if client_ID == 11:
			clt_ID[1] = 11
		elif client_ID == 12:
			clt_ID[2] = 12
		if client_ID == 1 and main_stj1_recv and msg_len<9000000:
			trj_ser_list[0].append([client_ID,data_number,msg_len,pose_x,pose_y])
			if len(trj_ser_list[0]) == msg_len:
				print('客户端【1】轨迹数据接受完毕')
				main_stj1_recv = False
				#print('*********',trj_ser_list[0],'**********')
				client1_recv_flag = True
			elif len(trj_ser_list[0]) > msg_len:
				print('再次收到新的轨迹！')
				trj_ser_list[0].clear()
				trj_ser_list[0].append([client_ID,data_number,msg_len,pose_x,pose_y])
		elif client_ID == 2 and main_stj2_recv and msg_len<9000000:
			trj_ser_list[1].append([client_ID,data_number,msg_len,pose_x,pose_y])
			if len(trj_ser_list[1]) == msg_len:
				main_stj2_recv = False
				print('客户端【2】轨迹数据接受完毕')
				#print('*********',trj_ser_list[1],'**********')
				client2_recv_flag = True
			elif len(trj_ser_list[1]) > msg_len:
				print('再次收到新的轨迹！')
				trj_ser_list[1].clear()
				trj_ser_list[1].append([client_ID,data_number,msg_len,pose_x,pose_y])
		elif clt_ID[1] == 11 and msg_len<9000000:
			if client1_recv_flag:
				print("发送客户端【1】的轨迹")
				for i in range(len(trj_ser_list[0])):
					trj_send1_pack = struct.pack('!3i2f',*trj_ser_list[0][i])
					client.send(trj_send1_pack)
					time.sleep(0.02)#目前测试还是有必要加个延时的，不然很容易卡在中间
				print("发送客户端【1】的轨迹完毕")
			elif client1_recv_flag == False:
					trj_recv_unfinished = [11,-100,0,0.0,0.0]
					trj_recv_unfinished_pack = struct.pack('!3i2f',*trj_recv_unfinished)
					client.send(trj_recv_unfinished_pack)
					print('客户端【1】的轨迹未接收完！')

			#TODO:给相应客户端发消息
			#client1_recv_flag = False
		elif clt_ID[2] == 12 and msg_len<9000000:
			if client2_recv_flag:
				print("发送客户端【2】的轨迹")
				for i in range(len(trj_ser_list[1])):
					trj_send1_pack = struct.pack('!3i2f',*trj_ser_list[1][i])
					client.send(trj_send1_pack)
					time.sleep(0.02)#目前测试还是有必要加个延时的，不然很容易卡在中间
				print("发送客户端【2】的轨迹完毕")
			elif client2_recv_flag == False:
					trj_recv_unfinished = [12,-100,0,0.0,0.0]
					trj_recv_unfinished_pack = struct.pack('!3i2f',*trj_recv_unfinished)
					client.send(trj_recv_unfinished_pack)
					print('客户端【2】的轨迹未接收完！')
			#client2_recv_flag = False
		elif msg_len == 9999999:
			print(data_number)
			if	client_ID == 0:
				clientall_state[0] = data_number
			elif client_ID == 1:
				clientall_state[1] = data_number
			elif client_ID == 2:
				clientall_state[2] = data_number
			print('收到状态请求指令！')
			print(clientall_state)
			if clientall_state[0] == 1 and clientall_state[1] == 1 and clientall_state[2] == 1:
				clientall_state_flag = [3,28,9999999,3.0,0.0]
				#11100 28 10100 11000
				clientall_state_flag_pack = struct.pack('!3i2f',*clientall_state_flag)
				client.send(clientall_state_flag_pack)
				print('三架无人机已准备好！')
			elif clientall_state[0] == 1 and clientall_state[1] == 1 and clientall_state[2] == 0:
				clientall_state_flag = [3,24,9999999,2.0,0.0]
				clientall_state_flag_pack = struct.pack('!3i2f',*clientall_state_flag)
				client.send(clientall_state_flag_pack)
				print('无人机0和1已准备好！')
			elif clientall_state[0] == 1 and clientall_state[1] == 0 and clientall_state[2] == 1:
				clientall_state_flag = [3,20,9999999,2.0,0.0]
				clientall_state_flag_pack = struct.pack('!3i2f',*clientall_state_flag)
				client.send(clientall_state_flag_pack)
				print('无人机0和2已准备好！')
			elif clientall_state[0] == 0 and clientall_state[1] == 1 and clientall_state[2] == 1:
				clientall_state_flag = [3,12,9999999,2.0,0.0]
				clientall_state_flag_pack = struct.pack('!3i2f',*clientall_state_flag)
				client.send(clientall_state_flag_pack)
				print('无人机1和2已准备好！')
			else:
				clientall_state_flag = [3,0,9999999,0.0,0.0]
				clientall_state_flag_pack = struct.pack('!3i2f',*clientall_state_flag)
				client.send(clientall_state_flag_pack)
				print('三架无人机至多一架准备好!')
		else:
			print('结束！')

if __name__ == '__main__':
	init()
	thread = Thread(target=accept_client)
	thread.setDaemon(True)
	thread.start()
	while True:
		use_cmd = input('')
		if use_cmd =='1':
			print('----当前在线人数：',len(client_conn_pool))
		elif use_cmd =='2':
			exit()
